package com.quick.util;

import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Collection;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.lang.Nullable;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.quick.Quick;
import com.quick.base.BaseUser;
import com.quick.config.QuickProperties;
import com.quick.config.QuickSystem;
import com.quick.config.TokenStorage;
import com.quick.exception.QuickException;
import com.quick.exception.QuickStateEnum;
import com.quick.service.IQuickService;
import com.quick.spring.SpringUtil;

public class QuickTool {
	/**
	 * 获取Token存储方式
	 * @return
	 */
	public static TokenStorage getStoreType() {
		Quick quick = SpringUtil.getBean(Quick.class);
		QuickProperties properties = (QuickProperties) quick.getDevelop().get("basis");
		return properties.getTokenStorage();
	}
	
	public static String getTokenKey() {
		Quick quick = SpringUtil.getBean(Quick.class);
		QuickProperties properties = (QuickProperties) quick.getDevelop().get("basis");
		return properties.getTokenKey();
	}
	
	public static HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
    }
	
	public static QuickSystem quickSystem() {
		return new QuickSystem();
	}
    
    public static void isNull(@Nullable Object value, String message) {
    	if(value == null) {
    		throw new IllegalArgumentException(message);
    	}
    }
    
    public static void isNullBlank(@Nullable Object value, String message) {
    	if(value == null) {
    		throw new IllegalArgumentException(message);
    	}
    	if(value instanceof String) {
    		String str = (String) value;
    		if(str.trim().length()<=0) {
    			throw new IllegalArgumentException(message);
    		}
    	}
    }

	public static void isTrue(boolean hasKey,String	 message) {
		if(hasKey) {
    		throw new IllegalArgumentException(message);
    	}
	}
	
	public static MapUtil map() {
		return new MapUtil();
	}
	/**
	 * 自定义异常
	 * @param flag
	 * @param tokenFailure
	 * @throws QuickException 
	 */
	public static void isTrue(Boolean flag, QuickStateEnum failure){
		if(flag) {
			throw new QuickException(failure);
    	}
	}
	/**
	 * 自定义空异常
	 * @param value
	 * @param tokenFailure
	 */
	public static void isNull(Object value, QuickStateEnum failure) {
		if(value == null) {
			throw new QuickException(failure);
		}
	}
	
	public static void notNull(Object value, QuickStateEnum failure) {
		if(value != null) {
			throw new QuickException(failure);
		}
	}

	public static void isNullBlank(String value, QuickStateEnum failure) {
		if(value == null) {
    		throw new QuickException(failure);
    	}
    	if(value instanceof String) {
    		String str = (String) value;
    		if(str.trim().length()<=0) {
    			throw new QuickException(failure);
    		}
    	}
	}

	public static void isFalse(Boolean flag, QuickStateEnum failure) {
		if(!flag) {
			throw new QuickException(failure);
    	}
	}
	
	public static boolean isNull(Collection<?> value) {
		if(value != null && value.size()<=0) {
			return true;
		}
		return false;
	}
	
	public static boolean notNull(Collection<?> value) {
		return !isNull(value);
	}
	
	public static boolean isNull(Object value) {
		if(value == null) {
			return true;
		}
		if(value instanceof Integer) {
			 Integer v = new Integer(0);
			if(v.equals(value)) {
				return true;
			}
		}
		if(value instanceof String) {
			String val = (String) value;
			if(val.trim().length()<=0) {
				return true;
			}
		}
		return false;
	}
	
	public static boolean notNull(Object value) {
		return !isNull(value);
	}
	
	
	public static ImgUtil img() {
		return new ImgUtil();
	}
	
	/**
	 * <p>分割字符串</p>
	 * @param split
	 * @param str
	 * @return
	 */
	public static String[] splitter(String split,String str) {
		return str.split(split);
	}
	
	/**
	 * <p>拼接字符串</p>
	 * @param join
	 * @param str
	 * @return
	 */
	public static String joiner(String join,Collection<?> str) {
		StringBuilder builder = new StringBuilder();
		for (Object e : str) {
			builder.append(e).append(join);
		}
		builder.deleteCharAt(builder.length()-1);
		return builder.toString();
	}
	
	public static void setFillUpdate(Object bean) {
		try {
			String fullKey = QuickSystem.getFullKey();
			IQuickService  quickService = SpringUtil.getBean(IQuickService.class);
			BaseUser baseUser =  quickService.getBaseUser(fullKey);
			BeanUtils.setProperty(bean, "updateTime", LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
			BeanUtils.setProperty(bean, "updateBy", baseUser.getId());
			BeanUtils.setProperty(bean, "updateName", baseUser.getName());
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
	
	
	public static void setFillInsert(Object bean) {
		try {
			String fullKey = QuickSystem.getFullKey();
			IQuickService  quickService = SpringUtil.getBean(IQuickService.class);
			BaseUser baseUser =  quickService.getBaseUser(fullKey);
			BeanUtils.setProperty(bean, "updateTime", LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
			BeanUtils.setProperty(bean, "updateBy", baseUser.getId());
			BeanUtils.setProperty(bean, "updateName", baseUser.getName());
			BeanUtils.setProperty(bean, "createTime", LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
			BeanUtils.setProperty(bean, "createBy", baseUser.getId());
			BeanUtils.setProperty(bean, "createName", baseUser.getName());
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
	}
}
